package yz.com.javautil.hutool;

import cn.hutool.core.date.DateUtil;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class TestDateUtil {
    public static void main(String[] args) {
//        System.out.println(getDateDifferenceValueByParam("year", "2023", "2020"));
//        System.out.println(getDateDifferenceValueByParam("month", "202304", "202001"));
//        System.out.println(getDateDifferenceValueByParam("day", "20230424", "20200101"));

//        System.out.println(getAdjustmentPartitionName("year", "2023", 1));
//        System.out.println(getAdjustmentPartitionName("month", "202304", 1));
//        System.out.println(getAdjustmentPartitionName("day", "20230424", 1));

//        List<String> clearedUpListByParam = getClearedUpListByParam("day", "data_", "20220501", "20220420");
//        System.out.println(clearedUpListByParam.toString());

        System.out.println("--------------[1]-------------"+formatMonthsToYears(1));
        System.out.println("--------------[2]-------------"+formatMonthsToYears(2));
        System.out.println("--------------[3]-------------"+formatMonthsToYears(3));
        System.out.println("--------------[4]-------------"+formatMonthsToYears(4));
        System.out.println("--------------[5]-------------"+formatMonthsToYears(5));
        System.out.println("--------------[6]-------------"+formatMonthsToYears(6));
        System.out.println("--------------[7]-------------"+formatMonthsToYears(7));
        System.out.println("--------------[8]-------------"+formatMonthsToYears(8));
        System.out.println("--------------[9]-------------"+formatMonthsToYears(9));
        System.out.println("--------------[10]-------------"+formatMonthsToYears(10));
        System.out.println("--------------[11]-------------"+formatMonthsToYears(11));
        System.out.println("--------------[12]-------------"+formatMonthsToYears(12));
        System.out.println("--------------[15]-------------"+formatMonthsToYears(15));
        System.out.println("--------------[19]-------------"+formatMonthsToYears(19));
        System.out.println("--------------[23]-------------"+formatMonthsToYears(23));
        System.out.println("--------------[25]-------------"+formatMonthsToYears(25));
        System.out.println("--------------[30]-------------"+formatMonthsToYears(30));

    }

    private static long getDateDifferenceValueByParam(String partitionType, String partitionMax, String partitionMin) {
        long differenceValue = 0;
        switch (partitionType) {
            case "year":
                differenceValue = DateUtil.betweenYear(DateUtil.parse(partitionMax, "yyyy"), DateUtil.parse(partitionMin, "yyyy"), false);
                break;
            case "month":
                differenceValue = DateUtil.betweenMonth(DateUtil.parse(partitionMax, "yyyyMM"), DateUtil.parse(partitionMin, "yyyyMM"), false);
                break;
            case "day":
                differenceValue = DateUtil.betweenDay(DateUtil.parse(partitionMax, "yyyyMMdd"), DateUtil.parse(partitionMin, "yyyyMMdd"), false);
                break;
            default:
                break;
        }
        return differenceValue;
    }

    private static String getAdjustmentPartitionName(String type, String partitionMax, int retentionYears) {
        String adjustPartitionName = "";
        switch (type) {
            case "year":
                adjustPartitionName = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(partitionMax, "yyyy"), -365 * retentionYears), "yyyy");
                break;
            case "month":
                adjustPartitionName = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(partitionMax, "yyyyMM"), -365 * retentionYears), "yyyyMM");
                break;
            case "day":
                adjustPartitionName = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(partitionMax, "yyyyMMdd"), -365 * retentionYears), "yyyyMMdd");
                break;
            default:
                break;
        }
        return adjustPartitionName;
    }

    private static List<String> getClearedUpListByParam(String type, String partitionPrefix, String partitionMax, String partitionMin) {
        List<String> clearedUpList = new ArrayList<>();
        switch (type) {
            case "year":
                LocalDateTime startYear = LocalDateTime.of(Integer.parseInt(partitionMin), 1, 1, 0, 0);
                LocalDateTime endYear = LocalDateTime.of(Integer.parseInt(partitionMax), 1, 1, 0, 0);
                long numOfYearsBetween = ChronoUnit.YEARS.between(startYear, endYear) - 1;
                if (numOfYearsBetween > 0) {
                    for (long i = 0; i <= numOfYearsBetween; i++) {
                        clearedUpList.add(partitionPrefix + DateUtil.format(startYear.plusYears(i), "yyyy"));
                    }
                }
                break;
            case "month":
                LocalDateTime startMonth = LocalDateTime.of(Integer.parseInt(partitionMin.substring(0, 4)), Integer.parseInt(partitionMin.substring(4)), 1, 0, 0);
                LocalDateTime endMonth = LocalDateTime.of(Integer.parseInt(partitionMax.substring(0, 4)), Integer.parseInt(partitionMax.substring(4)), 1, 0, 0);
                long numOfMonthsBetween = ChronoUnit.MONTHS.between(startMonth, endMonth) - 1;
                if (numOfMonthsBetween > 0) {
                    for (long i = 0; i <= numOfMonthsBetween; i++) {
                        clearedUpList.add(partitionPrefix + DateUtil.format(startMonth.plusMonths(i), "yyyyMM"));
                    }
                }
                break;
            case "day":
                LocalDateTime startDay = LocalDateTime.of(Integer.parseInt(partitionMin.substring(0, 4)), Integer.parseInt(partitionMin.substring(4, 6)), Integer.parseInt(partitionMin.substring(6)), 0, 0);
                LocalDateTime endDay = LocalDateTime.of(Integer.parseInt(partitionMax.substring(0, 4)), Integer.parseInt(partitionMax.substring(4, 6)), Integer.parseInt(partitionMax.substring(6)), 0, 0);
                long numOfDaysBetween = ChronoUnit.DAYS.between(startDay, endDay) - 1;
                if (numOfDaysBetween > 0) {
                    for (long i = 0; i <= numOfDaysBetween; i++) {
                        clearedUpList.add(partitionPrefix + DateUtil.format(startDay.plusDays(i), "yyyyMMdd"));
                    }
                }
                break;
            default:
                break;
        }
        return clearedUpList;
    }


    private static int formatMonthsToYears(int retentionMonths) {

        return (int) Math.ceil(retentionMonths * 1.0 / 12);
    }

}
